﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.OutboundOrder;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Add
{
    public class RowOutBundCreateHandler : IRequestHandler<RowOutBundCreateCommon, Result>
    {
        private readonly IBaseRepository<OutBoundLine> OutBoundLine;
        public IMapper mapper { get; set; }
        public RowOutBundCreateHandler(IBaseRepository<OutBoundLine> outBoundLine, IMapper mapper)
        {
            OutBoundLine = outBoundLine;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(RowOutBundCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<OutBoundLine>(request);
            string sql = "insert into OutBoundLine values(@OutBoundLineId,@InventoryInfoId,@Prices,@Nums,@Unit,@Summary,@OrdersId,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = OutBoundLine.Add(sql, new
            {
                AddData.OutBoundLineId,
                AddData.InventoryInfoId,
                AddData.Prices,
                AddData.Nums,
                AddData.Unit,
                AddData.Summary,
                AddData.OrdersId,
                AddData.IsDelete,
                AddData.Handle,
                AddData.HandleContext,
                AddData.HandleTime,
                AddData.Pid,
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
